<html>
<head>
  <title>Evernote Export</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="YXBJ Windows/600753 (zh-CN, DDL); Windows/10.0.0 (Win64);"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="738"/>

<div>
<span><div><div>CPU是通过地址访问内存，而不是通过变量名或函数民访问内存</div><div>c++把地址称为指针</div><div><br/></div><div>指针</div><div>1.节省内存</div><div>2.动态分配内存，直接访问内存地址</div><div>3.使程序简洁、紧凑、高效</div><div><br/></div><div><br/></div><ol><li><div>内存空间的访问方式</div></li><li><div>指针与指针变量</div></li><li><div>指针变量的定义和初始化</div></li><li><div>指针运算</div></li><li><div>指针与一维数组</div></li></ol><div>内存空间的访问方式</div><div>不同的类型数据占用的字节数不一样<img src="指针_files/Image.png" type="image/png" data-filename="Image.png" width="221"/></div><div>类型            占用字节</div><div>int                4</div><div>float            4</div><div>double        8</div><div>char            1</div><div><br/></div><div><br/></div><div>不用的操作系统与编译器所占用的字节有所偏差</div><div>比如 int 32操作系统  占4字节 一个字节8位 4*8=32，如果是64操作系统那么那么它的字节为8 一个字节位8位 8*8=64</div><div><br/></div><div><img src="指针_files/Image [1].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div>通过变量名的地址访问变量值</div></div><div><br/></div><div><span style="font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(192, 237, 124); color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">指针(Pointer)与指针变量</span></div><div><br/></div><div>用来存放地址的变量就成为指针变量</div><div>变量的指针，就是指该地址的地址因为c++中地址就是指针</div><div>未初始化的指针变量不能使用，否则会系统混乱，甚至死机</div><div><br/></div><div>不能用auto变量的地址去初始化static型指针</div><div><br/></div><div>auto变量方便</div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">比如int i=10; auto a = i; //这样a也是in</span><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">t类型了</span></div><div><br/></div><div>static 局部变量</div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">(1)静态</span><a href="https://www.baidu.com/s?wd=%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F&amp;tn=SE_PcZhidaonwhc_ngpagmjz&amp;rsv_dl=gh_pc_zhidao" style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(63, 136, 191); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">局部变量</a><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">在</span><a href="https://www.baidu.com/s?wd=%E5%87%BD%E6%95%B0&amp;tn=SE_PcZhidaonwhc_ngpagmjz&amp;rsv_dl=gh_pc_zhidao" style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(63, 136, 191); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">函数</a><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">内定义，但不像</span><a href="https://www.baidu.com/s?wd=%E8%87%AA%E5%8A%A8%E5%8F%98%E9%87%8F&amp;tn=SE_PcZhidaonwhc_ngpagmjz&amp;rsv_dl=gh_pc_zhidao" style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(63, 136, 191); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">自动变量</a><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">那样，当调用时就存在，退出</span><a href="https://www.baidu.com/s?wd=%E5%87%BD%E6%95%B0&amp;tn=SE_PcZhidaonwhc_ngpagmjz&amp;rsv_dl=gh_pc_zhidao" style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(63, 136, 191); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">函数</a><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">时就消失。静态</span><a href="https://www.baidu.com/s?wd=%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F&amp;tn=SE_PcZhidaonwhc_ngpagmjz&amp;rsv_dl=gh_pc_zhidao" style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(63, 136, 191); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">局部变量</a><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">始终存在着，也就是说它的</span><a href="https://www.baidu.com/s?wd=%E7%94%9F%E5%AD%98%E6%9C%9F&amp;tn=SE_PcZhidaonwhc_ngpagmjz&amp;rsv_dl=gh_pc_zhidao" style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(63, 136, 191); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">生存期</a><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">为整个</span><a href="https://www.baidu.com/s?wd=%E6%BA%90%E7%A8%8B%E5%BA%8F&amp;tn=SE_PcZhidaonwhc_ngpagmjz&amp;rsv_dl=gh_pc_zhidao" style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(63, 136, 191); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">源程序</a><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">。 　　</span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">(2)静态</span><a href="https://www.baidu.com/s?wd=%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F&amp;tn=SE_PcZhidaonwhc_ngpagmjz&amp;rsv_dl=gh_pc_zhidao" style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(63, 136, 191); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">局部变量</a><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">的</span><a href="https://www.baidu.com/s?wd=%E7%94%9F%E5%AD%98%E6%9C%9F&amp;tn=SE_PcZhidaonwhc_ngpagmjz&amp;rsv_dl=gh_pc_zhidao" style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(63, 136, 191); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">生存期</a><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">虽然为整个</span><a href="https://www.baidu.com/s?wd=%E6%BA%90%E7%A8%8B%E5%BA%8F&amp;tn=SE_PcZhidaonwhc_ngpagmjz&amp;rsv_dl=gh_pc_zhidao" style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(63, 136, 191); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">源程序</a><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">，但是其</span><a href="https://www.baidu.com/s?wd=%E4%BD%9C%E7%94%A8%E5%9F%9F&amp;tn=SE_PcZhidaonwhc_ngpagmjz&amp;rsv_dl=gh_pc_zhidao" style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(63, 136, 191); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">作用域</a><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">仍与</span><a href="https://www.baidu.com/s?wd=%E8%87%AA%E5%8A%A8%E5%8F%98%E9%87%8F&amp;tn=SE_PcZhidaonwhc_ngpagmjz&amp;rsv_dl=gh_pc_zhidao" style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(63, 136, 191); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">自动变量</a><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">相同，即只能在定义该变量的</span><a href="https://www.baidu.com/s?wd=%E5%87%BD%E6%95%B0&amp;tn=SE_PcZhidaonwhc_ngpagmjz&amp;rsv_dl=gh_pc_zhidao" style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(63, 136, 191); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">函数</a><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">内使用该变量。退出该函数后， 尽管该变量还继续存在，但不能使用它。 　　　　</span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">(3)对基本类型的静态局部变量若在说明时未赋以初值，则系统自动赋予0值。而对</span><a href="https://www.baidu.com/s?wd=%E8%87%AA%E5%8A%A8%E5%8F%98%E9%87%8F&amp;tn=SE_PcZhidaonwhc_ngpagmjz&amp;rsv_dl=gh_pc_zhidao" style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(63, 136, 191); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">自动变量</a><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">不赋初值，则其值是不定的。</span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">根据局部变量的特点可以看出他是一种生存期为整个源文件的量</span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br/></span></div><div>为什么不能初始化static变量</div><div>s <span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">tatic变量是程序接收的时候才释放对象的，但它不需要手动释放。</span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">static如果在一个函数内申明，这每次进入这个函数时，还是使用第一次声明的变量，并且还保存的上次使用的值(auto变量这时函数结束是即释放了，再次调用这个函数是，有重新定义了一个新的变量）</span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; font-variant-caps: normal; font-variant-ligatures: normal;">static变量如果在结构和类中使用，这结构或类定义的一切对象，都将共享唯一static变量。</span></div><div><span style="font-size: 12pt;"><br/></span></div><div><span style="font-size: 12pt; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;;">通过指针变量取得数据</span></div><div><span style="font-size: 12pt; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;;">*称为指针运算符，用来取得某个地址上的数据</span></div><div><span style="font-size: 12pt; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;;">*p 等价于a</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div>       int a = 10;    </div><div>       int *p;</div><div>       p = &amp;a;</div><div>       cout &lt;&lt; *p&lt;&lt;p&lt;&lt;&amp;a;</div><div><span style="font-size: 9pt; color: rgb(51, 51, 51); font-family: Monaco;">输出结果：</span>10 00000035F30FF934 00000035F30FF934</div></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">现在可以看出 p代表的是变量a的地址 *p代表地址与指针的值，p此时的地址与a的地址保持一致</span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br/></span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">定义指针变量时必须带*</span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">给指针变量赋值时不能带*</span><span style="font-size: 12pt;"><img src="指针_files/Image [2].png" type="image/png" data-filename="Image.png" width="239"/></span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br/></span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">指针可以获取内存上的数据，同时也可以修改内存的数据</span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br/></span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div>       int a = 15, b = 99, c = 222;</div><div>       int *p = &amp;a;</div><div>       cout &lt;&lt; *p;//15</div><div>       *p = b;//通过*p获取指针上的值并被赋值原本的a值被覆盖</div><div>       c = *p;//通过*p获取指针上的值并赋值</div><div>       cout &lt;&lt; a &lt;&lt; b &lt;&lt; c &lt;&lt; *p;//99 99 99 99</div></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br/></span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">定义指针变量*是说明符,表明p是指针变量</span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">使用指针变量前的*是指针运算符，表示获取指针指向的数据（地址代表的数据）</span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br/></span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">&amp;：取地址运算符</span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">一般形式：&amp;变量名</span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">作用：取一个内存变量的地址（变量指针）</span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br/></span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">*：指针运算符</span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">一般形式：*指针变量名</span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">作用：通过指针变量间接访问指针变量所指向变量的数据</span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br/></span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br/></span></div><div><span style="font-size: 16px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">&amp;与*的运算符关系</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div>       int a = 15;</div><div>       int *p = &amp;a;</div><div>       cout &lt;&lt; *&amp;a;</div><div>       //15</div><div>       //拆分为两部分*&amp;a = *(&amp;a)=*(p)=15</div></div><div><span style="font-size: 12pt;"><img src="指针_files/Image [3].png" type="image/png" data-filename="Image.png" width="68"/></span></div><div><span style="font-size: 12pt;">从图中可以看出&amp;a起到了一个获取地址当作指针的作用，指针前面加*就是平时看到的*p</span></div><div><span style="font-size: 12pt;"><br/></span></div><div><span style="font-size: 12pt;">如果反过来呢？</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div>       int a = 15;</div><div>       int *p = &amp;a;</div><div>       cout &lt;&lt; &amp;*p;</div><div>       //地址</div><div>       //先取值在通过&amp;取到值的地址（变量指针）</div></div><div><span style="font-size: 12pt;"><img src="指针_files/Image [4].png" type="image/png" data-filename="Image.png" width="54"/></span></div><div><span style="font-size: 12pt;">图中他们的结果是一样的只是执行的顺序发生了变化</span></div><div><span style="font-size: 12pt;"><img src="指针_files/Image [5].png" type="image/png" data-filename="Image.png"/></span></div><div><span style="font-size: 12pt;"><img src="指针_files/Image [6].png" type="image/png" data-filename="Image.png" width="182"/>&amp;和*运算时为逆运算</span></div><div><span style="font-size: 12pt;"><br/></span></div><div><span style="font-size: 12pt;">指针变量的加法运算</span></div><div><span style="font-size: 12pt;"><br/></span></div><div><span style="font-size: 12pt;">int,char,double类型的指针变量每次加1</span></div><div><span style="font-size: 12pt;">他们的地址分别增加了4,1,8（对应占位符）</span></div><div><span style="font-size: 12pt;"><br/></span></div><div><span style="font-size: 12pt;">如果b为指针指向a</span></div><div><span style="font-size: 12pt;">b++；使地址加4正好可以跳过整数a跳过4个占位符</span></div><div><span style="font-size: 12pt;"><br/></span></div><div><span style="font-size: 12pt;">如果指针指向一个是数组那么加一是有意义的，但是如果指向一个变量虽然他的占位符是四，加一之后在第二个位置编译器不会报错但是没有任何意义。</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div>       int a[3] = { 2,4,6 }, i;</div><div>       int *p = a;//如果把数组变量名给了指针，那么就会把元素首地址给了指针</div><div>       for (i = 0;i &lt; 3;i++)</div><div>       {</div><div>              cout &lt;&lt; *(p + i) &lt;&lt; '\t';</div><div>       }</div><div>//2 4 6</div></div><div><font style="font-size: 14pt;"><span style="direction: ltr; unicode-bidi: embed; font-size: 14pt; color: black; font-family: 微软雅黑; font-weight: bold; line-height: 150%;">数组中的所有元素在内存中是连续排列的，加</span><span style="direction: ltr; unicode-bidi: embed; font-size: 14pt; color: black; font-family: 微软雅黑; font-weight: bold; line-height: 150%;">1</span><span style="direction: ltr; unicode-bidi: embed; font-size: 14pt; color: black; font-family: 微软雅黑; font-weight: bold; line-height: 150%;">就表示指向下一个元素，减</span><span style="direction: ltr; unicode-bidi: embed; font-size: 14pt; color: black; font-family: 微软雅黑; font-weight: bold; line-height: 150%;">1</span><span style="direction: ltr; unicode-bidi: embed; font-size: 14pt; color: black; font-family: 微软雅黑; font-weight: bold; line-height: 150%;">就表示指向上一个元素。这样，指针的加减运算就具有了现实的意义。</span></font></div><div><span style="direction: ltr; unicode-bidi: embed; font-size: 9pt;"><br/></span></div><div><span style="direction: ltr; unicode-bidi: embed; font-size: 9pt;"><br/></span></div><div><span style="font-size: 9pt;"><br/></span></div><div><span style="font-size: 14pt;">在平时传参的时候 也会遇到内存指针的问题</span></div><div><span style="font-size: 14pt;">如果是单纯的参数传递 没有任何参数指针</span><span style="font-size: 14pt;"><img src="指针_files/Image [7].png" type="image/png" data-filename="Image.png" width="175"/></span><span style="font-size: 14pt;"><img src="指针_files/Image [8].png" type="image/png" data-filename="Image.png" width="161"/></span></div><div><span style="font-size: 14pt;">可以看出 不管是形参还是实参，都是在内存中创建新的地址，内存不同但是值相同。</span></div></span>
</div></body></html> 